home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK1.toast / Development Kits (Disc 1) / PC Card SDKs / PC Card Manager 2.0 SDK / Software / CRM Sample / SerialPortUtilities / SerialPortUtilities.c next >
Encoding:
C/C++ Source or Header  |  1997-06-05  |  3.2 KB  |  120 lines  |  [TEXT/CWIE]

  1. /*
  2.      File:        SerialPortUtilities.c
  3.  
  4.      Contains:    Useful utility functions for dealing with serial ports registered
  5.                  with the Communications Resource Manager (CRM).
  6.                  
  7.                  PC Card modems, PowerBook Internal modems, and NuBus Serial port
  8.                  are the most common types of these serial devices.
  9.  
  10.                 A single function called:
  11.  
  12.                     void    ForEachSerialPort(SerialPortActionProcPtr actionProc,void * param);
  13.  
  14.                 allows you to record information about available serial ports. Your
  15.                 “actionProc” is called once for every serial port in the system and
  16.                 is passed the CRMSerialPtr for the corresponding device.
  17.                 
  18.                 We use this function as a building block for:
  19.  
  20.                     void    AddSerialPortsToMenu(MenuRef serialPortMenu);
  21.  
  22.                 which is very useful for building a popup or other menu from which
  23.                 the user can select an available serial port.
  24.  
  25.  
  26.                 Once you have a port name (either selected by the user or stored
  27.                 in a preferences file), you must get obtain the CRMSerialPtr in
  28.                 order to get the driver information and/or icon for a port. For
  29.                 this purpose, we include:
  30.  
  31.                     CRMSerialPtr    GetCRMSerialPtrForPort(StringPtr portName);
  32.  */
  33.  
  34. #include <CommResources.h>
  35. #include <CRMSerialDevices.h>
  36. #include <Errors.h>
  37. #include <Menus.h>
  38. #include <TextUtils.h>
  39.  
  40. #include "SerialPortUtilities.h"
  41.  
  42.  
  43. void
  44. ForEachSerialPort(SerialPortActionProcPtr actionProc,void * param)
  45.     {
  46.     CRMRec        prototypeCRMRec;
  47.     CRMRecPtr    foundCRMRecPtr;
  48.     
  49.     //    start searching at the first device
  50.     
  51.     prototypeCRMRec.crmDeviceType = crmSerialDevice;
  52.     prototypeCRMRec.crmDeviceID = 0;
  53.  
  54.     //    repeatedly call CRMSearch until we run out of serial devices
  55.     
  56.     while ((foundCRMRecPtr = CRMSearch(&prototypeCRMRec)) != NULL)
  57.         {
  58.         (actionProc)((CRMSerialPtr) foundCRMRecPtr->crmAttributes,param);
  59.  
  60.         prototypeCRMRec.crmDeviceID++;    //    find the next device
  61.         }
  62.     }
  63.  
  64.  
  65. //    AddSerialPortsToMenu
  66. //
  67. //    a simple routine which utilizes ForEachSerialPort to add items
  68.  
  69.  
  70. static void
  71. AddSerialPortToMenu(CRMSerialPtr serialPortInfo,MenuRef serialPortMenu)
  72.     {
  73.     //    Here’s something to watch out for:
  74.     //
  75.     //        The StringHandles in a CRMSerialRecord are often unlocked
  76.     //        passing an unlocked, dereferenced handle to the Mac Toolbox
  77.     //        can be bad, so were careful to ensure we "do the right thing"
  78.     
  79.     SInt8    hState = HGetState((Handle) serialPortInfo->name);
  80.     
  81.     HLock((Handle) serialPortInfo->name);
  82.     AppendMenu(serialPortMenu,*(serialPortInfo->name));
  83.  
  84.     HSetState((Handle) serialPortInfo->name,hState);
  85.     }
  86.  
  87. void
  88. AddSerialPortsToMenu(MenuRef serialPortMenu)
  89.     {
  90.     ForEachSerialPort((SerialPortActionProcPtr) AddSerialPortToMenu,NULL);
  91.     }
  92.  
  93.  
  94. CRMSerialPtr
  95. GetCRMSerialPtrForPort(StringPtr portName)
  96.     {
  97.     CRMRec            prototypeCRMRec;
  98.     CRMRecPtr        foundCRMRecPtr;
  99.     CRMSerialPtr    serialPortInfo = NULL;
  100.  
  101.     //    start searching at the first device
  102.     
  103.     prototypeCRMRec.crmDeviceType = crmSerialDevice;
  104.     prototypeCRMRec.crmDeviceID = 0;
  105.  
  106.     //    repeatedly call CRMSearch until we run out of serial devices
  107.     
  108.     while ((foundCRMRecPtr = CRMSearch(&prototypeCRMRec)) != NULL)
  109.         {
  110.         serialPortInfo = (CRMSerialPtr) foundCRMRecPtr->crmAttributes;
  111.  
  112.         if (EqualString(*(serialPortInfo->name), portName, true, true))
  113.             return serialPortInfo;
  114.  
  115.         prototypeCRMRec.crmDeviceID++;    //    find the next device
  116.         }
  117.  
  118.     return NULL;
  119.     }
  120.